package oct2013;

/**
 * @author Yinzi Chen
 * @date Nov 5, 2013
 */
public class ValidPalindrome {
    public boolean isPalindrome(String s) {
        int n = s.length();
        s = s.toLowerCase();
        int i = 0, j = n - 1;
        while (i < j) {
            while (i < j && !Character.isLetterOrDigit(s.charAt(i))) {
                i++;
            }
            while (i < j && !Character.isLetterOrDigit(s.charAt(j))) {
                j--;
            }
            if (i >= j)
                return true;
            if (s.charAt(i++) != s.charAt(j--))
                return false;
        }
        return true;
    }
}
